<template>
  <view>
    <u-form :model="form" ref="uForm" style="margin: 0 20rpx;">
      <u-form-item label-width="145"  label="*原密码" prop="oldPwd"><u-input type="password" v-model="form.oldPwd" /></u-form-item>
      <u-form-item label-width="145"  label="*新密码" prop="newPwd"><u-input type="password" v-model="form.newPwd" /></u-form-item>
      <u-form-item label-width="145"  label="*确认密码" prop="confirmPwd"><u-input type="password" v-model="form.confirmPwd" /></u-form-item>
    </u-form>
    <u-button @click="submit" type="primary">提交</u-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form: {
        oldPwd: '',
        newPwd: '',
        confirmPwd: ''
      },
      rules: {
        confirmPwd:[
          {
            // 自定义验证函数，见上说明
            validator: (rule, value, callback) => {
              // 上面有说，返回true表示校验通过，返回false表示不通过
              return value === this.form.newPwd;
            },
            required: true,
            message: '两次密码不一致',
            trigger: 'blur'
          }
        ],
        newPwd: [
          {
            required: true,
            message: '新密码不能为空',
            trigger: 'blur'
          }
        ],
        oldPwd: [
          {
            required: true,
            message: '原密码不能为空',
            trigger: 'blur'
          }
        ]
      },
    };
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods:{
    submit(){
      this.$refs.uForm.validate(valid => {
        if (valid) {
          this.$u.api.changePwd(this.form).then(res => {
            if (res.code === 200){
              this.$u.toast("修改成功！");
              uni.switchTab({
                url:'/pages/mine/mine'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">

</style>
